<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- 上述3个meta标签*必须*放在最前面，任何其他内容都*必须*跟随其后！ -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="renderer" content="webkit">
<meta name="referrer" content="origin">
<meta name="viewport" content="width=device-width,initial-scale=1">
<#assign base=request.contextPath />
<link rel="shortcut icon" href="${base}/styles/images/common/icon.ico" type="image/x-icon" />
<link rel="canonical" href="">
<title>成才网-后台管理系统</title>
<meta name="description" content="成才网-后台管理系统" />
<!-- 新 Bootstrap 核心 CSS 文件 -->
<link href="${base}/styles/common/bootstrap.min.css" rel="stylesheet">
<link href="${base}/styles/adminlte/AdminLTE.min.css" rel="stylesheet" />
<!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
<!--[if lt IE 9]>
  <script src="//cdn.bootcss.com/html5shiv/3.7.2/html5shiv.min.js"></script>
  <script src="//cdn.bootcss.com/respond.js/1.4.2/respond.min.js"></script>
<![endif]-->
<script src="${base}/scripts/common/jquery-2.2.0.min.js"></script>
<script src="${base}/scripts/common/bootstrap.min.js"></script>
<script src="${base}/scripts/common/bootstrapValidator.min.js"></script>
<script src="${base}/scripts/common/bootbox.min.js"></script>
<script src="${base}/scripts/common/jquery.validate.min.js"></script>
<script src="${base}/scripts/common/messages_cn.js"></script>
<script src="${base}/scripts/common/web-common.js"></script>
</head>
<body>
<div class="container">
	<div class="row" style="margin-top: 50px;">
		<div>
			<p style="text-align: center;"><font style="font-size: large;"><strong>成才网</strong>&nbsp;管理后台</font></p>
		</div>
		<!-- /.login-logo -->
		<div>
			<form id="myForm" method="post" class="form-horizontal">
				<div class="form-group">
					<label for="username" class="col-md-4 control-label">登录账号</label>
					<div class="col-md-4">
						<input type="text" class="form-control" id="username" name="username"
							placeholder="请输入账号名" required="required">
					</div>
				</div>
				<div class="form-group">
					<label for="password" class="col-md-4 control-label">密码</label>
					<div class="col-md-4">
						<input type="password" class="form-control" id="password"
							name="password" placeholder="请输入密码" required="required">
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-4 control-label">验证码</label>
					<div class="col-md-4">
						<input type="text" class="form-control" name="randomCode"
							id="randomCode" placeholder="请输入验证码" onblur="valify()" required="required"/>
					</div>
					<div>
						<span class="error"></span>
					</div>
				</div>
				<div class="form-group">
					<label class="col-md-4 control-label"></label>
					<div class="col-md-4">
						<input type="button" name="code" id="code" onclick="createCode()" />
						<a href="javascript:createCode();">看不清?点击图片换一换</a>
					</div>
				</div>
				<div class="form-group">
					<div class="col-md-offset-4 col-md-8">
						<button type="submit" class="btn btn-default">登录</button>
					</div>
				</div>
			</form>

		</div>
		<!-- /.login-box-body -->
	</div>
	<!-- /.login-box -->
	</div>
	</body>
	<script src="${base}/scripts/js/md5.js"></script>
	<script type="text/javascript">
	
	/* 验证码模块 */
	//设置一个全局的变量，便于保存验证码
	var code;
	function createCode() {
		//首先默认code为空字符串
		code = '';
		//设置长度，这里看需求，我这里设置了4
		var codeLength = 4;
		var codeV = document.getElementById('code');
		//设置随机字符
		var random = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 'A', 'B', 'C',
				'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
				'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
		//循环codeLength 我设置的4就是循环4次
		for (var i = 0; i < codeLength; i++) {
			//设置随机数范围,这设置为0 ~ 36
			var index = Math.floor(Math.random() * 36);
			//字符串拼接 将每次随机的字符 进行拼接
			code += random[index];
		}
		//将拼接好的字符串赋值给展示的Value
		codeV.value = code;
	}

	$(function() {
		$('#myForm').bootstrapValidator(
				{
					message : '该项不能为空',
					live : 'disabled',
					feedbackIcons : {
						validating : 'glyphicon glyphicon-refresh'
					},
					submitHandler : function(validator, form, submitButton) {
						if (valify() == true) {
							var url = '${base}/admin/auth/loginAction.json';
							var entity = {
								 username : $("#username").val(),
						         password : md5($("#password").val()),
						         randomCode : $('#randomCode').val().trim()
							};
							var onloadFunc = function(result) {
								if (result.isSuccess == true) {
									window.location = "${base}/admin/auth/index.do";
								} else {
									 bootbox.setLocale("zh_CN");
								        bootbox.alert(result.message, function(result){
											createCode();
								        });
								}
							};
							httpRequestPostSubmitJson(form, url, entity, false,
									onloadFunc);
						}
					} ,	fields : {
						username : {
								validators : {
									notEmpty : {
										message : '登录账户不能为空'
									},
									stringLength: {
				                        min: 2,
				                        max: 10,
				                        message: '账户长度在2-10个字符之间'
				                    }
								}
							},
							password : {
								validators : {
									notEmpty : {
										message : '密码不能为空'
									},
									stringLength: {
				                        min: 6,
				                        max: 12,
				                        message: '密码长度在6-12个字符之间'
				                    }
								}
							}
					}
				});
	});

	/***********验证码*********/
	function valify() {
		var oValue = $('#randomCode').val().trim().toUpperCase();
		if ($('#randomCode').val().trim() === '') {
			$('span.error').show();
			$('span.error').html('验证码不能为空！');
			createCode();
			return false;
		}
		if (oValue == code) {
			$('span.error').hide();
			return true;
		} else {
			$('span.error').show();
			$('span.error').html('验证码不正确！');
			createCode();
			return false;
		}
	}

	//设置此处的原因是每次进入界面展示一个随机的验证码，不设置则为空
	window.onload = function() {
		createCode();
	}
	</script>
</html>